package com.estar.worktime.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.estar.worktime.entity.TmblWorktimeEntity;
import com.estar.worktime.entity.WorkPlaceAndDataVo;
import com.estar.worktime.service.TmblWorktimeServiceI;

@Component("excelWorkTableList")
@Scope("prototype")
public class ExcelWorkTableList {
	
	@Autowired
	private SystemService systemService;
	private DateFormat dd=new SimpleDateFormat("yyyy-MM-dd");
	private DateFormat dt=new SimpleDateFormat("HH:mm:ss");
	private static final Logger logger = Logger.getLogger(ExcelWorkTableList.class);
	public void excelTable(String title, HttpServletRequest request, HttpServletResponse response,List<TmblWorktimeEntity> workList){
		
		response.setContentType("application/vnd.ms-excel");
		OutputStream fOut = null;
		try{
				String browse = BrowserUtils.checkBrowse(request);
				if ("MSIE".equalsIgnoreCase(browse.substring(0, 4))) {
					response.setHeader("content-disposition",
							"attachment;filename=" + java.net.URLEncoder.encode(title, "UTF-8") + ".xls");
				} else {
					String newtitle = new String(title.getBytes("UTF-8"), "ISO8859-1");
					response.setHeader("content-disposition",
							"attachment;filename=" + newtitle + ".xls");//设置标题
				}
				HSSFWorkbook workbook = exportExcel(title,workList);
				fOut = response.getOutputStream();
				workbook.write(fOut);
		
		}catch(Exception e){
			logger.error("打印排班表系统错误：",e);
		}finally{
			if(fOut!=null){
				try {
					fOut.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
		public HSSFWorkbook exportExcel(String title,List<TmblWorktimeEntity> workList){
		// 声明一个工作薄
			HSSFWorkbook wb = new HSSFWorkbook();
				try {
					
					// 生成一个表格
					Sheet sheet = wb.createSheet(title);
					// 设表名为中文名字,要ENCODING_UTF_16编码， 默认编码是用 ENCODING_COMPRESSED_UNICODE
					
					int rowIndex = 0;
					// 创建行
					// 创建字体
					Font font1 = createFont(wb,14, Font.BOLDWEIGHT_BOLD);
					Font font2 = createFont(wb,8, HSSFFont.BOLDWEIGHT_NORMAL,
							HSSFColor.GREY_80_PERCENT.index);
					Font font3 = createFont(wb,10, HSSFFont.BOLDWEIGHT_BOLD);
					Font font4 = createFont(wb,11, HSSFFont.BOLDWEIGHT_BOLD,
							HSSFColor.BLUE.index);
					// 创建式样
					CellStyle style2 = createStyle(wb,font2, HSSFCellStyle.BORDER_THIN,
							HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
							HSSFCellStyle.BORDER_THIN, HSSFCellStyle.ALIGN_CENTER,
							HSSFCellStyle.VERTICAL_CENTER, HSSFColor.BLUE.index,
							HSSFColor.RED.index, (short) 0);
					style2.setWrapText(true);

					CellStyle style4 = createStyleThinFrame(wb,font4,
							HSSFColor.GREY_25_PERCENT.index,
							HSSFColor.GREY_25_PERCENT.index);
					style4.setWrapText(true);
					
					Font font2Red = createFont(wb,8, HSSFFont.BOLDWEIGHT_NORMAL,
							HSSFColor.GREY_80_PERCENT.index);
					font2Red.setColor(HSSFColor.RED.index);
					CellStyle styleRed = createStyle(wb,font2Red, HSSFCellStyle.BORDER_THIN,
							HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
							HSSFCellStyle.BORDER_THIN, HSSFCellStyle.ALIGN_CENTER,
							HSSFCellStyle.VERTICAL_CENTER, HSSFColor.BLUE.index,
							HSSFColor.RED.index, (short) 0);
					styleRed.setWrapText(true);
					
					Row row2 = createRow(sheet, rowIndex);
					row2.setHeight((short) 400);
					Cell cell = createCell(row2, 0, "      ");
					cell.setCellStyle(style4);
					
					sheet.setColumnWidth(0, 5000);
					int s=6000;
					List<String> lieNames= lieName();
					for(int i=0;i<(lieNames.size());i++){
						Cell cell1 = createCell(row2, i, lieNames.get(i));
						cell1.setCellStyle(style4);
						sheet.setColumnWidth(i, s);//设置列宽
					}
					int index = 1;
					for(TmblWorktimeEntity wp:workList){
						
						rowIndex++;
						int celIndex =0;
						Row rowList = createRow(sheet, rowIndex);
						rowList.setHeight((short) 400);//设置行高
						List<String> v=value(wp);
						int size=v.size();
						for(int j=0;j<size;j++){
							Cell cellList = createCell(rowList, celIndex+j,v.get(j));
							if(StringUtil.isNotEmpty(v.get(j)) && (v.get(j).equals("是") || v.get(j).equals("替班"))){
								cellList.setCellStyle(styleRed);
							}else{
								cellList.setCellStyle(style2);
							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				return wb;
			}
		
		
		private List<String> lieName(){
			List<String> lieName=new ArrayList<String>();
			lieName.add("员工号");
			lieName.add("员工姓名");
			lieName.add("工作地点");
			lieName.add("工作日期");
			lieName.add("工作时长");
			lieName.add("上班时间");
			lieName.add("是否迟到");
			lieName.add("迟到时间");
			lieName.add("下班时间");
			lieName.add("是否早退");
			lieName.add("早退时长");
			lieName.add("上班缺卡");
			lieName.add("下班缺卡");
			lieName.add("上班状态");
			lieName.add("工作状况");
			lieName.add("替班情况");
			return lieName;
			
		}
		
		private List<String> value(TmblWorktimeEntity w){
			List<String> values=new ArrayList<String>();
			values.add(w.getEmpcdoe());
			values.add(w.getEmpname());
			values.add(w.getWorkplace());
			if(w.getWorkdate()!=null){
				values.add(dd.format(w.getWorkdate()));
			}else{
				values.add("00:00:00");
			}
			values.add(w.getWorktime());
			
			if(w.getOndutytime()!=null){
				values.add(dt.format(w.getOndutytime()));
			}else{
				values.add("00:00:00");
			}
			if(StringUtil.isNotEmpty(w.getLateflage())){
				if(w.getLateflage().equals("t")){
					values.add("是");
				}else{
					values.add("否");
				}
			}else{
				values.add("");
			}
			
			values.add(w.getLatetime());
			if(w.getOffdutytime()!=null){
				values.add(dt.format(w.getOffdutytime()));
			}else{
				values.add("00:00:00");
			}
			
			if(StringUtil.isNotEmpty(w.getLeaveearlyflage())){
				if(w.getLeaveearlyflage().equals("t")){
					values.add("是");
				}else{
					values.add("否");
				}
			}else{
				values.add("");
			}
			
			values.add(w.getLeaveearlytime());
			
			if(StringUtil.isNotEmpty(w.getOndutycard())){
				if(w.getOndutycard().equals("t")){
					values.add("是");
				}else{
					values.add("否");
				}
			}else{
				values.add("");
			}
			
			if(StringUtil.isNotEmpty(w.getOffdutycard())){
				if(w.getOffdutycard().equals("t")){
					values.add("是");
				}else{
					values.add("否");
				}
			}else{
				values.add("");
			}
			
			
			
			if(StringUtil.isNotEmpty(w.getWork())){
				String ww=w.getWork();
				if(ww.equals("t")){
					values.add("正常");
				}else if (ww.equals("f")) {
					values.add("旷工");
				}else if (ww.equals("oc")) {
					values.add("上班缺卡");
				}else if (ww.equals("fc")) {
					values.add("下班缺卡");
				}else if (ww.equals("l")) {
					values.add("迟到");
				}else{
					values.add("否");
				}
			}else{
				values.add("");
			}
			
			if(StringUtil.isNotEmpty(w.getState())){
				if(w.getState().equals("3")){
					values.add("替班");
				}else{
					values.add("正常");
				}
			}else{
				values.add("");
			}
			
			if(StringUtil.isNotEmpty(w.getReplaceName())){
				
				values.add(w.getReplaceName());
				
			}else{
				values.add("");
			}
			return values;
			
		}
		/*public Font createFontBold() {
			return createFont(14, Font.BOLDWEIGHT_BOLD);
		}*/
		public Font createFont(HSSFWorkbook wb,int height, int boldWeight) {
			// fWorkbook = wb.createFont();
			Font font = wb.createFont();
			font.setFontHeightInPoints((short) height);
			font.setBoldweight((short) boldWeight);
			font.setFontName("Arial");

			return font;
		}
		public Font createFont(HSSFWorkbook wb,int height, int boldWeight, int color) {
			Font font = wb.createFont();
			font.setFontHeightInPoints((short) height);
			font.setBoldweight((short) boldWeight);
			font.setColor((short) color);
			font.setFontName("Arial");

			return font;
		}
		public CellStyle createStyle(HSSFWorkbook wb,Font font, int bottom, int top, int left,
				int right, int halign, int valign, int fore, int back, int pattern) {
			CellStyle style = wb.createCellStyle();
			style.setFont(font);
			style.setBorderBottom((short) bottom);
			style.setBorderTop((short) top);
			style.setBorderLeft((short) left);
			style.setBorderRight((short) right);
			style.setAlignment((short) halign);
			style.setVerticalAlignment((short) valign);
			style.setFillBackgroundColor((short) back);
			style.setFillForegroundColor((short) fore);
			style.setFillPattern((short) pattern);
			return style;
		}
		public CellStyle createStyleThinFrame(HSSFWorkbook wb,Font font3, int fore, int back) {
			CellStyle style = createStyle(wb,font3, HSSFCellStyle.BORDER_THIN,
					HSSFCellStyle.BORDER_THIN, HSSFCellStyle.BORDER_THIN,
					HSSFCellStyle.BORDER_THIN, HSSFCellStyle.ALIGN_CENTER,
					HSSFCellStyle.VERTICAL_CENTER, fore, back,
					HSSFCellStyle.SOLID_FOREGROUND);
			return style;
		}
		public Row createRow(Sheet sheet1, int rowIndex) {
			return sheet1.createRow(rowIndex);
		}
		public Cell createCell(Row row2, int cellIndex, String value) {
			Cell cell = row2.createCell((short) cellIndex);
			// 显示中文。
			// cell.setEncoding(HSSFCell.ENCODING_UTF_16);
			cell.setCellValue(value);
			return cell;
		}
}
